<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/utente/UserApp.js"></script>
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
    <style>
      .ui-dialog .ui-dialog-titlebar
      {
        background: cornflowerblue;
      }

      .ui-tabs-nav
      {
        background: cornflowerblue;
      }

      .us-profile
      {
        background: activecaption;
      }
    </style>


    <script>
      var userApp;
            
      $(document).ready(function(){
        userApp = new UserApp();
        userApp.designRegistrationForm();

       // $('.us-divLogout').hide();

        $('.us-BtnLogout').click(function(){
          var parameters = {};
          parameters.operation="logout";
          $.ajax({
            type: "POST",
            url: "listeners/UtenteListener.php",
            dataType : "json",
            data: parameters,
            cache: false,
            success: function(dataResult)
            {
              if(dataResult==0)
              {
                $('.us-divLogin').show();
                $('.us-divLogout').hide();
              }

            },
            failure: function()
            {
              alert("Fault in logout");
            }
          });
        });
        
        $('.us-loginBtnLogin').click(function(){
          var parameters = {};
          parameters.email=$('.us-loginEmail', '.us-divLogin').val();
          parameters.password=$('.us-loginPassword', '.us-divLogin').val();
          parameters.operation="login";
          $.ajax({
            type: "POST",
            url: "listeners/UtenteListener.php",
            dataType : "json",
            data: parameters,
            cache: false,
            success: function(dataResult)
            {
                        
              if(dataResult.codice != "-1" && dataResult.codice != "-2")
              {
//                $('.us-divLogin').hide();
//                $('.us-divLogout').show();
//                $('.us-nome').html(dataResult.nome);
//                $('.us-cognome').html(dataResult.cognome);

              }
              else if(dataResult.codice == '-1')
                $('.us-divLogin').html("Utente inesistente");
              else if(dataResult.codice == '-2')
                $('.us-divLogin').html("Password non valida");

            },
            failure: function()
            {
              alert("Fault in login");
            }
          });
        })
      });
    </script>
  </head>
  <body>
    <div id="content"></div>
    <?php if (!isset($_COOKIE["MTOCodice"])) { ?>
      <div class="us-divLogin">
        Email<input type="text" class="us-loginEmail" />
        Password<input type="password" class="us-loginPassword" />
        <input type="button" value="Login" class="us-loginBtnLogin" />
      </div>
    <?php } else { ?>
      <div class="us-divLogout">
        Benvenuto 
        <input type="button" value="Logout" class="us-BtnLogout" />
    <input type="button" onclick="$('#content').empty();javascript:userApp.showUserProfile('<?= $_COOKIE["MTOCodice"]?>');" value="Show Profile"/>

      </div>
    <?php } ?>
  </body>
</html>
